package com.max.back.pharmacy.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.max.back.common.http.ResponseResult;

import com.max.back.common.sercurity.anno.AuthIgrone;
import com.max.back.common.sercurity.utils.ConfigrarionBean;
import com.max.back.common.sercurity.utils.JwtTokenUtil;
import com.max.back.common.sercurity.utils.RedisUtil;
import com.max.back.entity.CodeName;
import com.max.back.entity.Doctor;
import com.max.back.entity.DoctorLevel;
import com.max.back.pharmacy.form.DoctorForm;
import com.max.back.pharmacy.form.LoginForm;
import com.max.back.pharmacy.service.CodeNameService;
import com.max.back.pharmacy.service.DoctorLevelService;
import com.max.back.pharmacy.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/system")
public class SystemController {
  @Autowired private CodeNameService codeNameService;
  @Autowired private DoctorService doctorService;
  @Autowired private DoctorLevelService doctorLevels;
  @Autowired private JwtTokenUtil jwtUtil;
  @Autowired private RedisUtil redisUtil;
  @Autowired private ConfigrarionBean config;
  //  0.登录
  @AuthIgrone
  @PostMapping("/login")
  public ResponseResult login(@RequestBody @Validated LoginForm loginForm) {
    LambdaQueryWrapper<Doctor> lqw = new LambdaQueryWrapper<Doctor>();
    lqw.eq(Doctor::getUserName, loginForm.getUserName());
    lqw.eq(Doctor::getPassword, loginForm.getPassword());
    lqw.eq(Doctor::getDeleted, false);
    Doctor doctor = this.doctorService.getOne(lqw);
    if (doctor == null) return ResponseResult.getErrorResult("登录失败！用户名或密码错误。");
    DoctorLevel doctorLevel = this.doctorLevels.getById(doctor.getDoctorLevelId());
    String token = jwtUtil.createToken(doctor.getUserName());

    redisUtil.set(
        "login_" + doctor.getUserName(), doctor.getUserName(), config.getRedisExpiration());
    DoctorForm doctorForm = new DoctorForm(doctor, doctorLevel.getName(), token);
    List<DoctorForm> list = new ArrayList<>();
    list.add(doctorForm);
    return ResponseResult.getSuccessResult(list, "登录成功！", null);
  }

  // 1.获取所有常数项列表
  @GetMapping("/getCodeNameList")
  public ResponseResult getCodeNameList() {
    List<CodeName> list = this.codeNameService.list();
    if (list.size() == 0) return ResponseResult.getMessageResult(list, "常数项列表为空！");
    return ResponseResult.getSuccessResult(list, "获取常数项列表成功！", null);
  }
  ;
  // 2.获取用户对象列表
  @GetMapping("/getUser")
  public ResponseResult getUser(@RequestParam String id) {

    LambdaQueryWrapper<Doctor> lqw = new LambdaQueryWrapper<>();
    lqw.eq(Doctor::getId, id);
    Doctor doctor = this.doctorService.getById(id);
    if (doctor == null || doctor.getDeleted()) return ResponseResult.getErrorResult("用户不存在！");
    DoctorLevel doctorLevel = this.doctorLevels.getById(doctor.getDoctorLevelId());
    if (doctorLevel == null || doctorLevel.getDeleted())
      return ResponseResult.getErrorResult("医生级别不存在！");
    DoctorForm doctorForm = new DoctorForm(doctor, doctorLevel.getName(), "");
    List<DoctorForm> list = new ArrayList<>();
    list.add(doctorForm);
    return ResponseResult.getSuccessResult(list, "获取用户对象成功！", null);
  }
  // 3.修改密码
  @PostMapping("/updatePassword")
  public ResponseResult updatePassword(@RequestBody @Validated DoctorForm doctorForm) {
    Doctor doctor = this.doctorService.getById(doctorForm.getId());
    if (doctor == null || doctor.getDeleted()) return ResponseResult.getErrorResult("用户不存在！");
    doctor.setPassword(doctorForm.getPassword());
    doctor.setGmtModify(new Date());
    boolean flag = this.doctorService.updateById(doctor);
    if (flag) return ResponseResult.getMessageResult(null, "修改密码成功！");
    return ResponseResult.getErrorResult("修改密码失败！");
  }
  // 修改用户个人信息
  @PostMapping("/updateUserInfo")
  public ResponseResult updateUserInfo(@RequestBody @Validated DoctorForm doctorForm) {
    Doctor doctor = this.doctorService.getById(doctorForm.getId());
    if (doctor == null || doctor.getDeleted()) return ResponseResult.getErrorResult("用户不存在！");
    doctor.setUserName(doctorForm.getUserName());
    doctor.setRealName(doctorForm.getRealName());
    doctor.setPhone(doctorForm.getPhone());
    doctor.setGmtModify(new Date());
    boolean flag = this.doctorService.updateById(doctor);
    if (flag) return ResponseResult.getMessageResult(null, "修改个人信息成功！");
    return ResponseResult.getErrorResult("修改个人信息失败！");
  }
}
